<?php

namespace App\Store\Models\Order;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\Account;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsStyle;
use App\Store\Models\Sysset\Classesset;

class OrderCommissionLog extends Model
{
    protected $table = 'site_order_commission_log';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['shoppingaccountinfo1','shoppingaccountinfo2','storeinfo','goodsstyleinfo','classessetinfo'])
        ->where(['cp_id' => $cp_id,'red_status'=>1,'audit_status'=>2])->orderBy('create_at', 'desc');
        
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }
        $store_id = intval($request->store_id);
        if ($store_id>0) {
            $query->where('store_id',$store_id);
        }

        $type = intval($request->type);
        if ($type>0) {
            $query->where('type',$type);
        }
        $classesset_id = intval($request->classesset_id);
        if ($classesset_id>0) {
            $query->where('classesset_id',$classesset_id);
        }



        $goods_style_id = intval($request->goods_style_id);
        if ($goods_style_id>0) {
            $query->where('goods_style_id',$goods_style_id);
        }

       

        $order_sn=$request->order_sn;
        if ($order_sn != '') {
            $query->where('order_sn', 'like', '%' . $order_sn . '%');
        }

        $shopping_account_id1 = intval($request->shopping_account_id1);
        if ($shopping_account_id1>0) {
            $query->where('shopping_account_id1',$shopping_account_id1);
        }

        $shopping_account_id2 = intval($request->shopping_account_id2);
        if ($shopping_account_id2>0) {
            $query->where('shopping_account_id2',$shopping_account_id2);
        }

        $shopping_account_id = intval($request->shopping_account_id);
        if ($shopping_account_id>0) {
            $query->where('shopping_account_id1',$shopping_account_id)->orWhere('shopping_account_id2',$shopping_account_id);
        }

        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('order_at', '>=', $start_time)->where('order_at', '<=', $end_time);
        }

        $isgetall = intval($request->isgetall) ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            $list= Common::paginateDataToArray($list);
        } else {
            $list['list'] = $query->get()->toArray();

        }

        foreach ($list['list'] as &$item){
            if($shopping_account_id>0){
                if($item['shopping_account_id1']!=$shopping_account_id){
                    $item['commission1']=0;
                }
                if($item['shopping_account_id2']!=$shopping_account_id){
                    $item['commission2']=0;
                }

            }
        }

        return $list;
    }

    public function shoppingaccountinfo1(){
        
        return $this->hasOne(Account::class, 'id', 'shopping_account_id1');
    }
    public function shoppingaccountinfo2(){
        
        return $this->hasOne(Account::class, 'id', 'shopping_account_id2');
    }

    public function storeinfo(){
        
        return $this->hasOne(SiteStore::class, 'id', 'store_id');
    }

    public function goodsstyleinfo(){
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id');
    }

    //classesset_id
    public function classessetinfo(){
        return $this->hasOne(Classesset::class, 'id', 'classesset_id');
    }
}
